package cc.orange.mainView;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import cc.orange.entity.AllGradeInfo;

import cc.orange.databinding.ActivityGradeListBinding;

import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import cc.orange.R;
import cc.orange.adapter.GradeListItemAdapter;
import cc.orange.base.BaseActivity;
import cc.orange.entity.IsLoginsEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.ZXToastUtil;

public class GradeListActivity extends BaseActivity {

    private ActivityGradeListBinding binding;

    private GradeListItemAdapter adapter;

    private List<AllGradeInfo.DataDTO> data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_grade_list);
        binding.infosetImg3.setOnClickListener(view -> finish());

        data = new ArrayList<>();

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.rvGradeList.setLayoutManager(layoutManager);
        adapter = new GradeListItemAdapter(data);
        binding.rvGradeList.setAdapter(adapter);

        getAllGrade();

    }

    private void getAllGrade() {
        RetrofitFactory.create(ApiService.class)
                .levelAll(getTokens())
                .enqueue(new DefaultCallback<AllGradeInfo>() {
                    @Override
                    public void onStart(Call<AllGradeInfo> call) {
                        showLoading_base(GradeListActivity.this);
                    }

                    @Override
                    public void onError(Call<AllGradeInfo> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<AllGradeInfo> call, AllGradeInfo allGradeInfo) {
                        cancelLoading();
                        if (allGradeInfo.getCode() == 0) {

                            List<AllGradeInfo.DataDTO> dataAll = allGradeInfo.getData();
                            data.clear();
                            data.addAll(dataAll);
                            if (data!= null && data.size() > 0) {
                                adapter.updateData(data);
                            }
                        } else if (allGradeInfo.getCode() == 401) {
                            EventBus.getDefault().post(new IsLoginsEntity(false));
                        } else {
                            ZXToastUtil.showToast(allGradeInfo.getMsg());
                        }
                    }
                });
    }

    @Override
    protected void onPause() {
        super.onPause();
        TalkingDataSDK.onPageEnd(this, getLocalClassName());
    }

    @Override
    protected void onResume() {
        super.onResume();
        TalkingDataSDK.onPageBegin(this, getLocalClassName());
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
